package com.ruoyi.pay.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pay.domain.MchPayPassage;
import com.ruoyi.pay.domain.PayWay;
import com.ruoyi.pay.mapper.MchPayPassageMapper;
import com.ruoyi.pay.mapper.PayWayMapper;
import com.ruoyi.pay.service.PayWayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * 支付方式管理类
 *
 * @author zhuxiao
 * @site
 * @date 2021-04-27 15:50
 */
@RestController
@RequestMapping("/api/pay/payWays")
public class PayWayController extends BaseController {


    @Resource
    MchPayPassageMapper baseMapper;


    @Autowired
    PayWayService payWayService;
   @Resource
   PayWayMapper payWayMapper;
	/**
	 * @Author: ZhuXiao
	 * @Description: list
	 * @Date: 15:52 2021/4/27
	*/
	@GetMapping("/list")
	public TableDataInfo<PayWay> list(PageQuery pageQuery, PayWay payWay) {

		LambdaQueryWrapper<PayWay> condition = PayWay.gw();
		if(StringUtils.isNotEmpty(payWay.getWayCode())){
			condition.like(PayWay::getWayCode, payWay.getWayCode());
		}
		if(StringUtils.isNotEmpty(payWay.getWayName())){
			condition.like(PayWay::getWayName, payWay.getWayName());
		}
		condition.orderByAsc(PayWay::getWayCode);

        Page<PayWay> page = payWayMapper.selectPage(pageQuery.build(), condition);
        return TableDataInfo.build(page);
	}


    /** 查询商户对应应用下支持的支付方式 **/
    @GetMapping("/appId/{appId}")
    public R<Set<String>> payWayList(@PathVariable("appId") String appId) {

        Set<String> payWaySet = new HashSet<>();
        baseMapper.selectList(
            MchPayPassage.gw().select(MchPayPassage::getWayCode)
                .eq(MchPayPassage::getAppId, appId)
                .eq(MchPayPassage::getState, "1" )
        ).stream().forEach(r -> payWaySet.add(r.getWayCode()));

        return R.ok(payWaySet);
    }





}
